package com.hinnotek.warehouse.ui

import android.graphics.Rect
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.*
import com.hinnotek.warehouse.databinding.ActivityMachineAccountDetailBinding
import com.hinnotek.warehouse.databinding.ItemQueryMachineDetailLayoutBinding
import com.hinnotek.warehouse.databinding.ItemQueryWarehouseDetailTypeLayoutBinding
import com.hinnotek.warehouse.viewmodel.MachineAccountViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_DETAIL
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder
import dagger.hilt.android.AndroidEntryPoint

/**
 * 台账列表
 */
@Route(path = ACTIVITY_WAREHOUSE_MACHINE_ACCOUNT_DETAIL)
@AndroidEntryPoint
class MachineAccountDetailActivity : BaseActivity<ActivityMachineAccountDetailBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<MachineDetailBean, ItemQueryMachineDetailLayoutBinding>
    private val data = InstructionsBean.getData()

    private lateinit var layoutManager: LinearLayoutManager
    private val qrList by lazy { arrayListOf<ImageViewInfo>() }

    private val viewModel by viewModels<MachineAccountViewModel>()
    var machineAccountBean: MachineAccountBean? = null
    var machineListBean: MachineListBean? = null

    var applyDeviceId = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_machine_account_detail)
        setTitle("台账汇总")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()

        machineAccountBean = intent.getSerializableExtra(MACHINE_CONTENT_LIST) as MachineAccountBean
        machineListBean = intent.getSerializableExtra(MACHINE_DETAIL_LIST) as MachineListBean

        bindingView.machineData = machineAccountBean
        bindingView.machineListData = machineListBean

        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))
        viewModel.autoRefresh()
        viewModel.applyDeviceId = intent.getIntExtra("applyDeviceId", 0)
    }

    override fun setData() {
        viewModel.machineAccountDetailPage.observe(this, Observer {
            for (machineDetailBean in it) {
                machineDetailBean.applyType = machineAccountBean?.applyType ?: ""
            }
            adapter.addAll(it, true)
        })
    }


    override fun initVew() {
        adapter = SimpleBindingAdapter(
            R.layout.item_query_machine_detail_layout,
            ArrayList(),
            BR.detailData
        )
        layoutManager = LinearLayoutManager(this)
        bindingView.recycler.layoutManager = layoutManager
        bindingView.recycler.adapter = adapter

        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.refresh()
        }

        //添加adapter
        adapter.addListener { binding, itemData ->
            binding.QRImage.setOnClickListener {
                PreviewBuilder.from(this)
                    .setImg(ImageViewInfo(itemData.productNumUrl, Rect()))
                    .setFullscreen(true)
                    .setSingleShowType(false)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .setType(PreviewBuilder.IndicatorType.Number)
                    .start()

            }
        }

        /*adapter.addListener { binding, itemData ->
            binding.QRImage.setOnClickListener {
                computeBoundsBackward(layoutManager.findFirstVisibleItemPosition(), binding.QRImage)
                PreviewBuilder.from(this@MachineAccountDetailActivity)
                    .setImgs(qrList)
                    .setCurrentIndex(adapter.getItemPosition(itemData))
                    .setSingleFling(true)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .setType(PreviewBuilder.IndicatorType.Number)
                    .start()
            }

            binding.enclosure.setOnClickListener {
                PreviewBuilder.from(this@MachineAccountDetailActivity)
                    .setImg(ImageViewInfo(itemData.enUrl,Rect()))
                    .setFullscreen(true)
                    .setSingleShowType(false)
                    .setPhotoFragment(PhotoFragment::class.java)
                    .setProgressColor(R.color.xui_config_color_main_theme)
                    .start()
            }
        }*/

    }

    /*  private fun computeBoundsBackward(firstCompletelyVisiblePos: Int, view: ImageView) {
          qrList.clear()
          for (bean in adapter.data) {
              qrList.add(ImageViewInfo(bean.qrUrl))
          }
          for (i in firstCompletelyVisiblePos until adapter.itemCount) {
              val bounds = Rect()
              view.getGlobalVisibleRect(bounds)
              qrList[i].bounds = bounds
          }
      }*/


}